package rexsee.cartoon;

import java.util.ArrayList;
import java.util.Collections;

import rexsee.cartoon.CartoonView.CartoonInterface;
import rexsee.cartoon.CartoonView.CartoonUnit;
import rexsee.cartoon.component.Cartoon;
import rexsee.cartoon.component.Cartoon.CompratorForCartoon;
import rexsee.cartoon.component.Scrolling;
import rexsee.core.browser.clazz.LogListeners;
import rexsee.xml.XmlElement;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.widget.Toast;

public class Cartoons implements CartoonInterface {

	public static final String EVENT_FRAME_PLAY = "onCartoonFramePlay";
	public static final String EVENT_MOVE_BLOCKED = "onCartoonMoveBlocked";
	public static final String EVENT_MOVE_FINISHED = "onCartoonMoveFinished";
	public static final String EVENT_ONSCROLLEDAT = "onCartoonScrolledAt";

	private final Parser parser;
	private final ArrayList<Cartoon> list = new ArrayList<Cartoon>();

	public Cartoons(Parser parser) {
		this.parser = parser;
		parser.browser.eventList.add(EVENT_FRAME_PLAY);
		parser.browser.eventList.add(EVENT_MOVE_BLOCKED);
		parser.browser.eventList.add(EVENT_MOVE_FINISHED);
		parser.browser.eventList.add(EVENT_ONSCROLLEDAT);
	}

	public void parse(XmlElement element) {

		if (element.childs == null || element.childs.size() == 0) return;

		for (int i = 0; i < element.childs.size(); i++) {
			XmlElement child = element.childs.get(i);
			if (child.tagName.equalsIgnoreCase(TagName.TAG_CARTOON)) {
				add(new Cartoon(parser, child));
			} else if (child.tagName.equalsIgnoreCase(TagName.TAG_SCROLLING)) {
				add(new Scrolling(parser, child));
			}
		}

	}

	public void remove(String id) {
		if (id == null) return;
		for (int i = 0; i < size(); i++) {
			Cartoon cartoon = get(i);
			if (id.equals(cartoon.getId())) list.remove(i);
		}
	}
	public Cartoon get(String id) {
		if (id == null) return null;
		for (int i = 0; i < size(); i++) {
			Cartoon cartoon = get(i);
			if (id.equals(cartoon.getId())) return cartoon;
		}
		return null;
	}

	public void sortByZ() {
		Collections.sort(list, new CompratorForCartoon());
	}
	public Cartoon get(int index) {
		return list.get(index);
	}
	public int size() {
		return list.size();
	}
	public void add(Cartoon cartoon) {
		if (cartoon == null) return;
		list.add(cartoon);
		parser.touchListener.registerTouchListener(cartoon.getCartoonTouchListener());
	}
	public void destroy() {
		for (int i = 0; i < list.size(); i++) {
			list.get(i).destroy();
		}
		list.clear();
	}

	@Override
	public ArrayList<CartoonUnit> getUnits(CartoonView view) {

		if (list.size() == 0) return null;

		try {

			Bitmap bitmap = Bitmap.createBitmap(parser.canvasWidth, parser.canvasHeight, Config.ARGB_8888);
			Canvas canvas = new Canvas(bitmap);

			sortByZ();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i) != null) list.get(i).draw(canvas);
			}

			CartoonUnit unit = new CartoonUnit();
			unit.bitmap = bitmap;
			unit.srcRect = null;
			unit.targetRect = null;
			unit.followSrcRect = false;
			unit.autoRecycle = true;
			ArrayList<CartoonUnit> units = new ArrayList<CartoonUnit>();
			units.add(unit);
			return units;

		} catch (Exception e) {

			String error = "Cartoon running error:" + e.getLocalizedMessage();
			parser.logListener.run(parser.context, parser.browser, LogListeners.LOG_TYPE_EXCEPTION, error);
			Toast.makeText(parser.context, error, Toast.LENGTH_SHORT).show();
			return null;

		}

	}

}
